package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.happyscan138.makefun.BaseAdapter.my_collection;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.View.RoundImageView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/8.
 */

public class myCollectionAdapter extends BaseAdapter {
    private  Context mContext;
    private List<my_collection> mdata;
    private String access_key;

    public myCollectionAdapter(Context mContext, List<my_collection> mdata,String access_key) {
        this.mContext = mContext;
        this.mdata = mdata;

        this.access_key=access_key;
    }


    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHodler_myCollection viewHolder;
        if(view==null){
            viewHolder=new ViewHodler_myCollection();
            view=View.inflate(mContext, R.layout.my_collection_item,null);
            viewHolder.myCollection_head=(RoundImageView) view.findViewById(R.id.my_collection_head);
            viewHolder.myCollection_name=(TextView) view.findViewById(R.id.my_collection_name);
            viewHolder.myCollection_time=(TextView) view.findViewById(R.id.my_collection_time);
         //   viewHolder.myCollection_comment=(TextView) view.findViewById(R.id.my_collection_comment);
            viewHolder.myCollection_image=(ImageView) view.findViewById(R.id.my_collection_image);
         //   viewHolder.myCollection_nicheng=(TextView) view.findViewById(R.id.my_collection_nicheng);
            viewHolder.myCollection_text=(TextView) view.findViewById(R.id.my_collection_text);
            viewHolder.myCollection_cancel=(Button)view.findViewById(R.id.CancelCollection);
            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHodler_myCollection) view.getTag();
        }
        my_collection bean=mdata.get(position);
        //通过viewHolder找到对应控件，避免每次通过findViewById找控件

        Picasso.with(mContext).load(bean.head).into(viewHolder.myCollection_head);
        viewHolder.myCollection_name.setText(bean.name);
        viewHolder.myCollection_time.setText(bean.time);
  //      viewHolder.myCollection_comment.setText(bean.comment);
        Picasso.with(mContext).load(bean.resource).into(viewHolder.myCollection_image);
 //       viewHolder.myCollection_nicheng.setText(bean.nicheng);
        viewHolder.myCollection_text.setText(bean.text);
        viewHolder.myCollection_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params=new RequestParams();
                params.put("access_key",access_key);
                params.put("like_id",mdata.get(position).like_id);
                log.e("cancel",params.toString());
                client.post(Urls.CancelCollection,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        log.e("jsonStr",jsonStr);
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            String code=jsonObject.getString("code");
                            int code_int=Integer.valueOf(code).intValue();
                            if(code_int==1) {
                                mdata.remove(position);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
        return view;
    }
}
class ViewHodler_myCollection{
    public RoundImageView myCollection_head;
    public TextView myCollection_name;
    public TextView myCollection_time;
 //   public TextView myCollection_comment;
    public ImageView myCollection_image;
 //   public TextView myCollection_nicheng;
    public TextView myCollection_text;
    public Button myCollection_cancel;
}